/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cundong.recyclerview.sample.utils;

import com.cundong.recyclerview.HeaderAndFooterRecyclerViewAdapter;
import com.cundong.recyclerview.ResourceTable;
import com.cundong.recyclerview.sample.slice.EndlessDirectionalLayoutAbilitySlice;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

/**
 * RecyclerViewStateUtils Class
 */
public class RecyclerViewStateUtils {
    private RecyclerViewStateUtils() {
        /* Do nothing */
    }

    /**
     * setFooterViewState
     *
     * @param instance context
     * @param listContainer listContainer
     * @param pageSize pageSize
     * @param state FooterView State
     * @param errorListener errorListener
     */
    public static void setFooterViewState(EndlessDirectionalLayoutAbilitySlice instance, ListContainer listContainer,
                                                int pageSize, LoadingFooter.State state,
                                                Component.ClickedListener errorListener) {
        if (instance == null) {
            return;
        }

        BaseItemProvider outerAdapter = listContainer.getItemProvider();

        if (outerAdapter == null) {
            return;
        }

        if (outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter =
                    (HeaderAndFooterRecyclerViewAdapter) outerAdapter;

            if (headerAndFooterAdapter.getInnerAdapter().getCount() < pageSize) {
                return;
            }

            if (state == LoadingFooter.State.TheEnd) {
                headerAndFooterAdapter.removeAllFooterView();
                headerAndFooterAdapter.addFooterView(
                        LayoutScatter.getInstance(instance).parse(ResourceTable.Layout_list_footer_end, null, false));
                return;
            }

            LoadingFooter footerView;

            if (headerAndFooterAdapter.getFooterViewsCount() > 0) {
                Component footerViewComponent = headerAndFooterAdapter.getFooterView();
                if (footerViewComponent instanceof LoadingFooter) {
                    footerView = (LoadingFooter) footerViewComponent;
                    footerView.setState(state);

                    listContainer.scrollTo(headerAndFooterAdapter.getCount() - 1);
                }
            } else {
                footerView = new LoadingFooter(instance);
                footerView.setState(state);
                headerAndFooterAdapter.addFooterView(footerView);
                listContainer.scrollTo(headerAndFooterAdapter.getCount() - 1);
            }
        }
    }

    /**
     * setFooterViewState
     *
     * @param listContainer listContainer
     * @param state state
     */
    public static void setFooterViewState(ListContainer listContainer, LoadingFooter.State state) {
        BaseItemProvider outerAdapter = listContainer.getItemProvider();
        if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            if (((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount() > 0) {
                Component footerViewComponent = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
                if (footerViewComponent instanceof LoadingFooter) {
                    LoadingFooter footerView = (LoadingFooter)footerViewComponent;
                    footerView.setState(state);
                }
            }
        }
    }

    /**
     * getFooterViewState
     *
     * @param listContainer listContainer
     * @return state state
     */
    public static LoadingFooter.State getFooterViewState(ListContainer listContainer) {
        BaseItemProvider outerAdapter = listContainer.getItemProvider();
        if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            if (((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount() > 0) {
                Component footerViewComponent = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
                if (footerViewComponent instanceof LoadingFooter) {
                    LoadingFooter footerView = (LoadingFooter) footerViewComponent;
                    return footerView.getState();
                }
            }
        }

        return LoadingFooter.State.Normal;
    }
}
